package com.project.app.server.visitorRecord;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.domain.entity.SysUser;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.SecurityUtils;
import com.project.server.domain.visitorRecord.TVisitorRecord;
import com.project.server.service.visitorRecord.ITVisitorRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.project.common.utils.DateUtils.dateTime;
import static com.project.common.utils.DateUtils.parseDateToStr;

/**
 * 小程序访客记录信息
 */
@RestController
@RequestMapping("/app/server/visitor")
public class ATVisitorRecordController extends BaseController {

    @Resource
    private ITVisitorRecordService visitorRecordService;

    /**
     * 获取待审批访客记录信息列表
     */
    //@PreAuthorize("@ss.hasPermi('server:visitor:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVisitorRecord visitorRecord) {
        startPage();
        List<TVisitorRecord> list = visitorRecordService.selectVisitorRecordList(visitorRecord);
        return getDataTable(list);
    }

    /**
     * 根据openId获取详细信息
     */
    @GetMapping(value = "/{openId}")
    public AjaxResult getInfo(TVisitorRecord visitorRecord) {
        return AjaxResult.success(visitorRecordService.selectVisitorRecordByOpenId(visitorRecord));
    }

    /**
     * @param visitorRecord 访客预约
     * @return
     */
    @Log(title = "访客记录管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TVisitorRecord visitorRecord) {
        return toAjax(visitorRecordService.insertVisitorRecord(visitorRecord));
    }

    /**
     * 审批访客记录
     */
    // @PreAuthorize("@ss.hasPermi('server:visitor:changeState')")
    @Log(title = "访客记录管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeState")
    public AjaxResult changeState(@Validated @RequestBody TVisitorRecord visitorRecord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        visitorRecord.setApproverId(user.getUserId());
        return toAjax(visitorRecordService.updateVisitorRecordApproverState(visitorRecord));
    }

}
